﻿using J.StatusModel;
using J.StatusModel.Dto;
using J.StatusRepository.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StatusService
{
    public class RoomTypeService : IRoomTypeService
    {
        private readonly IRoomTypeRepository roomTypeRepository;
        private readonly IRoomNumManageRepository roomNumManageRepository;

        public RoomTypeService(IRoomTypeRepository roomTypeRepository, IRoomNumManageRepository roomNumManageRepository)
        {
            this.roomTypeRepository = roomTypeRepository;
            this.roomNumManageRepository = roomNumManageRepository;
        }
        /// <summary>
        /// 房型添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> AddRoomType(RoomTypeModel model)
        {
            model.HotelId = 1;
            var result = await roomTypeRepository.AddAsync(model);
            return result;
        }

        /// <summary>
        /// 房型批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> DeleteRangeRoomType(string id)
        {
            var ids = id.Split(',');
            int count = 0;
            int counts = 0;
            //判断该房型下有没有房间,如果有不让删除
            foreach(var items in ids)
            {
                var numList = await roomNumManageRepository.GetAllAsync(x=>x.RoomId==Convert.ToInt32(items));
                counts+= numList.Count;
            }

            if(counts>0)
            {
                return -1;
            }
            else
            {
                foreach (var item in ids)
                {
                    var result = await roomTypeRepository.DeleteAsync(Convert.ToInt32(item));
                    count += result;
                }
                return count;
            }
        }
        /// <summary>
        /// 房型回显
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<RoomTypeModel> FindRoomType(int id)
        {
            //根据id找到指定数据
            var list = await roomTypeRepository.GetModelAsync(id);
            return list;
        }

        

        /// <summary>
        /// 返回房型列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="roomTypeName"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<(int totalCount, int pageCount, List<RoomTypeCountDto> list)> GetRoomType(int pageIndex, int pageSize, string? roomTypeName)
        {
            //找到所有房型数据
            var list = from a in await roomTypeRepository.GetAllAsync()
                       select new RoomTypeCountDto
                       {
                           ID = a.ID,
                           RoomTypeName = a.RoomTypeName,
                           RoomTypePrice = a.RoomTypePrice,
                           DepositPrice = a.DepositPrice,
                           NumCount = RoomNumCount(a.ID).Result,
                           RoomTypeNum = a.RoomTypeNum
                       };
            if(roomTypeName!="")
            {
                list = list.Where(x => x.RoomTypeName.Contains(roomTypeName)).ToList();
            }
            //计算数据总数
            var totalCount = list.Count();
            //计算总页数
            var pageCount = (int)Math.Ceiling((totalCount * 1.0) / pageSize);
            //倒序排序并且进行分页处理
            var result = list.OrderByDescending(x=>x.RoomTypeNum).Skip((pageIndex-1)*pageSize).Take(pageSize).ToList();
            return (totalCount, pageCount, result);
        }
        /// <summary>
        /// 返回所有房型列表
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<List<RoomTypeModel>> GetAllRoomType()
        {
            var list = await roomTypeRepository.GetAllAsync();
            return list;
        }
        /// <summary>
        /// 返回可定房号
        /// </summary>
        /// <param name="roomTypeId"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<List<RoomNumManage>> ReturnRoomNum(int roomTypeId)
        {
            var list = await roomNumManageRepository.GetAllAsync(x=>x.RoomId==roomTypeId&&x.RoomState==(RoomState)1);
            return list;
        }

        /// <summary>
        /// 返回房间数量
        /// </summary>
        /// <param name="roomTypeId">房型id</param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> RoomNumCount(int roomTypeId)
        {
            var list = await roomNumManageRepository.GetAllAsync(x=>x.RoomId==roomTypeId);
            return list.Count;
        }
        /// <summary>
        /// 返回房型dto
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<List<RoomTypeDto>> RoomTypeDto()
        {
            var roomTypeList = (from a in await roomTypeRepository.GetAllAsync()
                               join b in await roomNumManageRepository.GetAllAsync() on a.ID equals b.RoomId
                               select new RoomTypeDto
                               {
                                   ID = a.ID,
                                   RoomTypeName = a.RoomTypeName,
                                   RoomTypePrice = a.RoomTypePrice,
                                   RoomCount = roomNumManageRepository.GetAllAsync(x => x.RoomId == a.ID&&x.RoomState==(RoomState)1).Result.Count(),
                                   Disabled = roomNumManageRepository.GetAllAsync(x => x.RoomId == a.ID && x.RoomState == (RoomState)1).Result.Count()==0?false:true,
                                   CustomizePrice = a.RoomTypePrice,
                               }).DistinctBy(x=>x.ID);
            return roomTypeList.ToList();             
        }

        /// <summary>
        /// 房型修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> UpdateRoomType(RoomTypeModel model)
        {
            var result = await roomTypeRepository.UpdateAsync(model);
            return result;
        }
    }
}
